package speed2010.andreza.vspeed.ontologies;

import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;

import org.semanticweb.owl.apibinding.OWLManager;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOntologyCreationException;
import org.semanticweb.owl.model.OWLOntologyManager;

public class OntologyParser {
	
	private String ontologyPath;
	private Ontology ontology;
	
	public OntologyParser(Ontology ontology)
	{
		this.ontologyPath = ontology.getLocation();
		this.ontology = ontology;
	}
	
	public void start() throws OWLOntologyCreationException
	{
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();		
		URI ontologyURI = URI.create("file:///" + ontologyPath);		
		OWLOntology ontology = manager.loadOntologyFromPhysicalURI(ontologyURI);
		
		Iterator<OWLClass> it = ontology.getReferencedClasses().iterator();
				
		while(it.hasNext())
		{
			String concept = it.next().toString();
			OntologyConcept ontConcept = new OntologyConcept(concept);
			ArrayList<OntologyConcept> concepts = this.ontology.getConcepts();
			if(!concepts.contains(ontConcept))
			{
				concepts.add(ontConcept);				
			}
			else
			{
				int count = 0;
				boolean found = false;
				while(count < concepts.size() && !found)
				{
					if(ontConcept.equals(concepts.get(count)))
					{
						found = true;
					}
					else
					{
						count++;
					}
				}
				
				concepts.get(count).updateFrequency();
			}
		}
	}

}
